package dao

import (
	"hcy-api/structs/tables"
	"hcy-api/structs/vap"
	"vap/global"
)

var SessionRepository = new(sessionRepository)

type sessionRepository struct{}

func (r *sessionRepository) FindByHostIdAndWinId(hostId int64, winSessionId int64) (m tables.VapSession, err error) {
	err = global.DB.Model(&tables.VapSession{}).Where("host_id = ? and win_session_id = ? and hold_status != ?",
		hostId, winSessionId, tables.VaSessionHoldForHeld).Limit(1).Find(&m).Error
	return
}

func (r *sessionRepository) FindById(sessionId int64) (m tables.VapSession, err error) {
	err = global.DB.Where(&tables.VapSession{Id: sessionId}).First(&m).Error
	return
}

func (r *sessionRepository) UpdateById(m *tables.VapSession) (err error) {
	err = global.DB.Save(m).Error
	return err
}

func (r *sessionRepository) Create(m *tables.VapSession) (err error) {
	err = global.DB.Create(m).Error
	return
}

func (r *sessionRepository) FindPage(input vap.AdminSearchHostSession) (li []tables.VapSession, total int64, err error) {
	d := global.DB.Model(&tables.VapSession{})
	d.Where(" host_ip like '%" + input.HostIp + "%'")
	if input.Status != "" {
		d.Where(" status = ? ", input.Status)
	}
	if input.EndTime != 0 && input.StartTime != 0 {
		d.Where(" created_time > ? and created_time < ? ", input.StartTime, input.EndTime)
	}
	err = d.Count(&total).Limit(input.PageSize).Offset((input.PageNum - 1) * input.PageSize).Find(&li).Error
	return
}

func (r *sessionRepository) InsertMapHostSessionForApp(m *tables.MapHostSessionForApp) (err error) {
	err = global.DB.Create(m).Error
	return
}

func (r *sessionRepository) FindListBySessionIdsAndAppId(hostSessionIds []int64, appId int64) (li []tables.MapHostSessionForApp, err error) {
	err = global.DB.Model(tables.MapHostSessionForApp{}).Where("Host_session_id in ? and app_id = ?", hostSessionIds, appId).Find(&li).Error
	return
}

// SelectAppStartingTimesForActionSession 工具方法，检查主机上的活跃会话中是否启动过某个应用，尽量不要复用
func (r *sessionRepository) SelectAppStartingTimesForActionSession(hostId int64, appId int64) (num int64, err error) {
	err = global.DB.Raw(" select count(*) from vap_map_host_session_app WHERE "+
		" host_session_id IN ( select host_session_id from va_host_session WHERE host_id =? AND status !='disconnected' )"+
		" AND app_id = ? )", hostId, appId).Error
	return
}

func (r *sessionRepository) FindAppIssueById(id int64) (m tables.VapAppIssue, err error) {
	err = global.DB.Model(&tables.VapAppIssue{}).Where("id = ?", id).Find(&m).Error
	return
}

func (r *sessionRepository) DeleteAppInstallByHostId(hostId int64) (err error) {
	err = global.DB.Where("host_id = ?", hostId).Delete(&tables.VapAppInstall{}).Error
	return
}

func (r *sessionRepository) BatchInsertAppInstallList(li []tables.VapAppInstall) error {
	return global.DB.Create(&li).Error
}

func (r *sessionRepository) UpdateWindowSizeById(width, height string, id int64) error {
	session := tables.VapSession{}
	session.Width = width
	session.Height = height
	return r.UpdateById(&session)
}

func (r *sessionRepository) FindUserActionSession(userId, clusterId int64) (li []tables.VapSession, err error) {
	err = global.DB.Where("user_id = ? and cluster_id = ? and hold_status = ?", userId, clusterId, tables.VaSessionHoldForHolding).Find(&li).Error
	return
}

// FindActionSessionByAppId 根据应用ID查询活跃会话
func (r *sessionRepository) FindActionSessionByAppId(appId int64) (li []tables.VapSession, err error) {
	err = global.DB.Raw("SELECT * FROM vap_host_session WHERE id IN ( "+
		" SELECT host_session_id FROM vap_map_host_session_app WHERE host_session_id IN ( "+
		" SELECT host_session_id FROM vap_host_session WHERE hold_status = ? "+
		" ) AND app_id = ? )", tables.VaSessionHoldForHolding, appId).Scan(&li).Error
	return
}

func (r *sessionRepository) FindAppById(appId int64) (m tables.VapAppIssue, err error) {
	err = global.DB.Model(&tables.VapAppIssue{}).Where(&tables.VapAppIssue{Id: appId}).Find(&m).Error
	return
}
